package com.example.javatest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javatest.entity.domain.SupAddclass;
import com.example.javatest.entity.domain.TbClass;
import com.example.javatest.entity.domain.TbUser;
import com.example.javatest.mapper.TbClassMapper;
import com.example.javatest.mapper.TbUserMapper;
import com.example.javatest.service.InfoClassService;
import com.example.javatest.service.SupAddclassService;
import com.example.javatest.mapper.SupAddclassMapper;
import com.example.javatest.service.TbUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author cac_qin
* @description 针对表【sup_addclass】的数据库操作Service实现
* @createDate 2023-12-14 09:20:02
*/
@Service
public class SupAddclassServiceImpl extends ServiceImpl<SupAddclassMapper, SupAddclass>
    implements SupAddclassService{

    @Resource
    private TbUserMapper tbUserMapper;

    @Resource
    private TbClassMapper tbClassMapper;

    @Resource
    SupAddclassMapper supAddclassMapper;
    /**
     * 家长提交加入班级申请
     * @param className
     * @param userName
     * @return
     */
    @Override
    public boolean addClass(String className, String userName) {
        //查询用户是否存在
        QueryWrapper<TbUser> queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("user_Name",userName);
        int countUser = tbUserMapper.selectCount(queryWrapper1);
        if(countUser<0){
            return false;
        }
        //查询班级是否存在
        QueryWrapper<TbClass> queryWrapper2 =new QueryWrapper<>();
        queryWrapper2.eq("Class_Name",className);
        int countClass = tbClassMapper.selectCount(queryWrapper2);
        if(countClass<0){
            return false;
        }

        TbUser tbUser =tbUserMapper.selectOne(queryWrapper1);
        TbClass tbClass = tbClassMapper.selectOne(queryWrapper2);

        SupAddclass supAddclass = new SupAddclass();
        supAddclass.setAddClassid(tbClass.getClassId());
        supAddclass.setAddUser(tbUser.getUserName());

        boolean result = this.save(supAddclass);
        return result;
    }

    @Override
    public boolean handleAddClassAply(int cup_Id,String isHandle) {
        QueryWrapper<SupAddclass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cup_Id",cup_Id);
        int count = supAddclassMapper.selectCount(queryWrapper);
        if(count==0){
            return false;
        }

        //获取申请用户信息
        SupAddclass supAddclass =supAddclassMapper.selectOne(queryWrapper);
        String aplyUserName = supAddclass.getAddUser();
        QueryWrapper<TbUser> queryWrapperUser = new QueryWrapper<>();
        queryWrapperUser.eq("user_Name",aplyUserName);
        TbUser tbUser = tbUserMapper.selectOne(queryWrapperUser);

        if(isHandle.equals("NO")){
            supAddclass.setSupHandel("被驳回");
            supAddclassMapper.updateById(supAddclass);
            tbUser.setClassId(1000);
            tbUserMapper.updateById(tbUser);
        }
        if(isHandle.equals("YES")){
            supAddclass.setSupHandel("已通过");
            supAddclassMapper.updateById(supAddclass);
            tbUser.setClassId(supAddclass.getAddClassid());
            tbUserMapper.updateById(tbUser);
        }
        return true;
    }

}




